<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>部门管理</title>
    <#-- 使用相对当前模板文件的路径 再去找另一个模板文件 -->
    <#include "/common/link.ftl">

</head>
<body class="hold-transition skin-black sidebar-mini">
<div class="wrapper">
    <#include "/common/navbar.ftl">
    <!--定义一个变量  用于菜单回显-->
    <#assign currentMenu="department"/>

    <#include "/common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>部门管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <form class="form-inline" id="searchForm" action="/department/list" method="post">
                    <input type="hidden" name="currentPage" id="currentPage" value="1">
                    <a href="javascript:;" id="addbtn" class="btn btn-success btn-input" style="margin: 10px">
                        <span class="glyphicon glyphicon-plus"></span> 添加
                    </a>
                </form>
                <!--编写内容-->
                <div class="box-body table-responsive ">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>部门名称</th>
                            <th>部门电话</th>
                            <th>部门负责人</th>
                            <th>在编人数</th>
                            <th>在职人数</th>
                            <th>部门编号</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>

                        <#list result.list as tmp>
                            <tr>
                                <td>${tmp_index+1}</td>
                                <td>${tmp.name}</td>
                                <td>${(tmp.tel?c)!}</td>
                                <td>${tmp.functionary}</td>
                                <td>${tmp.aurhorized}</td>
                                <td>${tmp.staff}</td>
                                <td>${tmp.sn}</td>
                                <td>
                                    <a data-json='${tmp.json}' href="javascript:;"
                                       class="btn btn-info btn-xs btn-input btnedit">
                                        <span class="glyphicon glyphicon-pencil"></span> 编辑
                                    </a>
                                    <a deptId="${tmp.id}" class="btn btn-danger btn-xs btn-delete btndel"
                                       href="javascript:;">
                                        <span class="glyphicon glyphicon-trash"></span> 删除
                                    </a>
                                </td>
                            </tr>
                        </#list>
                        </tbody>
                    </table>
                    <!--分页-->
                    <#include "/common/page.ftl" >
                </div>
            </div>
        </section>
    </div>
    <!-- 模态框（Modal） -->
    <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel">
                        新增/编辑
                    </h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form" action="/department/saveOrUpdate" method="post"
                          id="addForm">
                        <input type="hidden" id="id" name="id">
                        <div class="form-group">
                            <label for="firstname" class="col-sm-4 control-label">部门名称</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="name" name="name"
                                       placeholder="请输入部门名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-4 control-label">部门电话</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="tel" name="tel"
                                       placeholder="请输入部门电话">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-4 control-label">部门负责人</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="functionary" name="employee.name"
                                       placeholder="请输入部门负责人">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-4 control-label">在编人数</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="aurhorized" name="aurhorized"
                                       placeholder="请输入在编人数">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-4 control-label">在职人数</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="staff" name="staff"
                                       placeholder="请输入在职人数">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-4 control-label">部门编码</label>
                            <div class="col-sm-5">
                                <input type="text" class="form-control" id="sn" name="sn"
                                       placeholder="请输入部门编码">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消
                    </button>
                    <button type="button" class="btn btn-primary" id="saveOrUpdate">
                        保存
                    </button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <#include "/common/footer.ftl" >
</div>
<script>
    $("#addbtn").click(function () {
        $("#addForm")[0].reset();
        $("#addModal").modal("show")
    })

    $("#saveOrUpdate").click(function () {
        $("#addForm").submit();
    })

    $(".btnedit").click(function () {
        console.log($(this))
        var dept = $(this).data("json");
        $("#id").val(dept.id)
        $("#name").val(dept.name)
        $("#tel").val(dept.tel)
        $("#functionary").val(dept.functionary)
        $("#aurhorized").val(dept.aurhorized)
        $("#staff").val(dept.staff)
        $("#sn").val(dept.sn)

        $("#addModal").modal("show")
    })
    $(".btndel").click(function () {
        var deptId = $(this).attr("deptId");
        console.log(deptId);
        Swal.fire({
            title: '确定删除?',
            text: "删除后无法恢复",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定删除!',
            cancelButtonText: '取消!'
        }).then((result) => {
            if (result.value) {
                //点击确认按钮后做的事情
                $.get("/department/delete", {id: deptId}, function (data) {
                    if (data.success) {
                        Swal.fire({
                            title: '删除成功',
                            text: "删除成功",
                            icon: 'success',
                            timer: 2000,
                        }).then((result) => {
                            window.location.href = "/department/list";
                        })
                    }
                })
            }
        })
    })
</script>
</body>
</html>
